package com.huida.app.java8.sort;

import cn.hutool.core.util.ArrayUtil;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author wangzhuangzhuang
 * @DESC:
 * @date 2022-01-24 10:32
 */
public class TestSort {
	/**
	 * 需求： 对一个Employee对象组成的List集合先按照性别字段在按照年龄倒序进行排序。
	 * SELECT *
	 * FROM employee
	 * ORDER BY gender DESC, age DESC;
	 **/

	private static void sortListDemo() {
		List<String> cities = Arrays.asList(
				"Milan",
				"london",
				"San Francisco",
				"Tokyo",
				"New Delhi"
		);
		// [Milan, london, San Francisco, Tokyo, New Delhi]
		System.out.println(ArrayUtil.toString(cities));

		cities.sort(String.CASE_INSENSITIVE_ORDER); // 首字母排序
		// [london, Milan, New Delhi, San Francisco, Tokyo]
		System.out.println(ArrayUtil.toString(cities));

		cities.sort(Comparator.naturalOrder()); // 自然排序
		// [Milan, New Delhi, San Francisco, Tokyo, london]
		System.out.println(ArrayUtil.toString(cities));

	}

	private static void sortAssignedFieldDemo() {
		Employee e1 = new Employee(1, 23, "M", "Rick", "Beethovan");
		Employee e2 = new Employee(2, 13, "F", "Martina", "Hengis");
		Employee e3 = new Employee(3, 43, "M", "Ricky", "Martin");
		Employee e4 = new Employee(4, 26, "M", "Jon", "Lowman");
		Employee e5 = new Employee(5, 19, "F", "Cristine", "Maria");
		Employee e6 = new Employee(6, 15, "M", "David", "Feezor");
		Employee e7 = new Employee(7, 68, "F", "Melissa", "Roy");
		Employee e8 = new Employee(8, 79, "M", "Alex", "Gussin");
		Employee e9 = new Employee(9, 15, "F", "Neetu", "Singh");
		Employee e10 = new Employee(10, 45, "M", "Naveen", "Jain");
		List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
		employees.sort(Comparator.comparing(Employee::getAge)); // 正序
		employees.forEach(System.out::println);
		System.out.println("===================================================================");
		employees.sort(Comparator.comparing(Employee::getAge).reversed()); // 倒序
		employees.forEach(System.out::println);
	}

//	Employee(id=2, age=13, gender=F, firstName=Martina, lastName=Hengis)
//	Employee(id=6, age=15, gender=M, firstName=David, lastName=Feezor)
//	Employee(id=9, age=15, gender=F, firstName=Neetu, lastName=Singh)
//	Employee(id=5, age=19, gender=F, firstName=Cristine, lastName=Maria)
//	Employee(id=1, age=23, gender=M, firstName=Rick, lastName=Beethovan)
//	Employee(id=4, age=26, gender=M, firstName=Jon, lastName=Lowman)
//	Employee(id=3, age=43, gender=M, firstName=Ricky, lastName=Martin)
//	Employee(id=10, age=45, gender=M, firstName=Naveen, lastName=Jain)
//	Employee(id=7, age=68, gender=F, firstName=Melissa, lastName=Roy)
//	Employee(id=8, age=79, gender=M, firstName=Alex, lastName=Gussin)
//	===================================================================
//	Employee(id=8, age=79, gender=M, firstName=Alex, lastName=Gussin)
//	Employee(id=7, age=68, gender=F, firstName=Melissa, lastName=Roy)
//	Employee(id=10, age=45, gender=M, firstName=Naveen, lastName=Jain)
//	Employee(id=3, age=43, gender=M, firstName=Ricky, lastName=Martin)
//	Employee(id=4, age=26, gender=M, firstName=Jon, lastName=Lowman)
//	Employee(id=1, age=23, gender=M, firstName=Rick, lastName=Beethovan)
//	Employee(id=5, age=19, gender=F, firstName=Cristine, lastName=Maria)
//	Employee(id=6, age=15, gender=M, firstName=David, lastName=Feezor)
//	Employee(id=9, age=15, gender=F, firstName=Neetu, lastName=Singh)
//	Employee(id=2, age=13, gender=F, firstName=Martina, lastName=Hengis)

	private static void sortWithChainedStyleDemo() {
		Employee e1 = new Employee(1, 23, "M", "Rick", "Beethovan");
		Employee e2 = new Employee(2, 13, "F", "Martina", "Hengis");
		Employee e3 = new Employee(3, 43, "M", "Ricky", "Martin");
		Employee e4 = new Employee(4, 26, "M", "Jon", "Lowman");
		Employee e5 = new Employee(5, 19, "F", "Cristine", "Maria");
		Employee e6 = new Employee(6, 15, "M", "David", "Feezor");
		Employee e7 = new Employee(7, 68, "F", "Melissa", "Roy");
		Employee e8 = new Employee(8, 79, "M", "Alex", "Gussin");
		Employee e9 = new Employee(9, 15, "F", "Neetu", "Singh");
		Employee e10 = new Employee(10, 45, "M", "Naveen", "Jain");
		List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);
		employees.sort(Comparator.comparing(Employee::getGender)
				.thenComparing(Employee::getAge)
				.reversed());
		System.out.println(ArrayUtil.toString(employees));
//		[Employee(id=8, age=79, gender=M, firstName=Alex, lastName=Gussin), Employee(id=10, age=45, gender=M, firstName=Naveen, lastName=Jain), Employee(id=3, age=43, gender=M, firstName=Ricky, lastName=Martin), Employee(id=4, age=26, gender=M, firstName=Jon, lastName=Lowman), Employee(id=1, age=23, gender=M, firstName=Rick, lastName=Beethovan), Employee(id=6, age=15, gender=M, firstName=David, lastName=Feezor), Employee(id=7, age=68, gender=F, firstName=Melissa, lastName=Roy), Employee(id=5, age=19, gender=F, firstName=Cristine, lastName=Maria), Employee(id=9, age=15, gender=F, firstName=Neetu, lastName=Singh), Employee(id=2, age=13, gender=F, firstName=Martina, lastName=Hengis)]
	}

	public static void main(String[] args) {
//		sortListDemo();
//		sortAssignedFieldDemo();
		sortWithChainedStyleDemo();
	}

}